import { findBuffer } from '../utils/Utils';
import { H264CodecID, getH264SPSInfo } from './H264_Parse';
import { H265CodecID, getH265SPSInfo } from './H265_Parse';
export const GBH264StreamType: number = 0x1B;
export const GBH265StreamType: number = 0x24;

export interface StreamVideoInfo {
    codec_id?: number;
    rate?: number;
    pix_fmt?: number;
    width?: number;
    height?: number;
    extradata?: string;
    extradata_size?: number;
}

export interface StreamAudioInfo {
    codec_id?: number;
    sample_fmt?: number;
    channels?: number;
    sample_rate?: number;
    rate?: number;
    extradata?: string;
    extradata_size?: number;
}

export interface StreamInfo {
    video: StreamVideoInfo;
    audio: StreamAudioInfo;
}

export function getVideoInfo(streamType: number, keyPayload: Buffer) : StreamInfo{
    let ret: StreamInfo = { video: {}, audio: {} }

    if(streamType == GBH264StreamType){
        ret.video.codec_id = H264CodecID;
        ret.video.rate = 90000;
        // 000000016742001f9da814016e9b808080a000007d000004e21080
        // 0000000168ce3c80
        // 0000000106e501a180
        // 0000000165.........
        let spsPos = findBuffer(keyPayload,Buffer.from([0x00,0x00,0x00,0x01,0x67]));
        let ppsPos = findBuffer(keyPayload,Buffer.from([0x00,0x00,0x00,0x01,0x68]));
        let idePos = findBuffer(keyPayload,Buffer.from([0x00,0x00,0x00,0x01,0x65]));
        let spsBuffer = keyPayload.slice(spsPos+4,ppsPos);
        let extradataBuffer = keyPayload.slice(spsPos,idePos);
        let spsInfo = getH264SPSInfo(spsBuffer);
        ret.video.pix_fmt = spsInfo.pix_fmt;
        ret.video.width = spsInfo.width;
        ret.video.height = spsInfo.height;
        ret.video.extradata = extradataBuffer.toString('hex');
        ret.video.extradata_size = extradataBuffer.byteLength;
    }
    if(streamType == GBH265StreamType){
        // 0000000140010c01ffff016000000300b0000003000003007bac09
        // 00000001420101016000000300b0000003000003007ba003c08010e58dae4932f9
        // 000000014401c0f2f03c90
        // 000000014e01e50429db000080
        // 000000012601..........
        ret.video.codec_id = H265CodecID;
        ret.video.rate = 90000;
        let vpsPos = findBuffer(keyPayload,Buffer.from([0x00,0x00,0x00,0x01,0x40,0x01]));
        let spsPos = findBuffer(keyPayload,Buffer.from([0x00,0x00,0x00,0x01,0x42,0x01]));
        let ppsPos = findBuffer(keyPayload,Buffer.from([0x00,0x00,0x00,0x01,0x44,0x01]));
        let idePos = findBuffer(keyPayload,Buffer.from([0x00,0x00,0x00,0x01,0x26,0x01]));
        let spsBuffer = keyPayload.slice(spsPos+4,ppsPos);
        let extradataBuffer = keyPayload.slice(vpsPos,idePos);
        let spsInfo = getH265SPSInfo(spsBuffer);
        ret.video.pix_fmt = spsInfo.pix_fmt;
        ret.video.width = spsInfo.width;
        ret.video.height = spsInfo.height;
        ret.video.extradata = extradataBuffer.toString('hex');
        ret.video.extradata_size = extradataBuffer.byteLength;
    }

    return ret;
}